%********************************************************
%** This program is specifically written to replicate  **
%** Xtreg in Hansford and Gomez's (2010) APSR article  **
%** Created by Min-Hua Huang, Jan 25, 2012             **
%** Model: Original                                    **
%********************************************************
clear; clc; format shortG;
load HansfordGomez_Data.mat;
code=HansfordGomez_Data(:,2);
y=HansfordGomez_Data(:,27);
grandmeany=mean(y);
n=size(y,1); 
x0=[HansfordGomez_Data(:,26) HansfordGomez_Data(:,3) HansfordGomez_Data(:,25) HansfordGomez_Data(:,29) HansfordGomez_Data(:,11:23)];
grandmeanx0=mean(x0);
m=size(x0,2);
obs=(1:n)';
replicate=[y x0 code obs];
replicate=sortrows(replicate,19);
%****** Replicate xtreg ******
dmean=dmean1(replicate,19);
y2=[dmean(:,1) replicate(:,19:20)];
mean4a=[(replicate(:,1:18)-dmean) replicate(:,19:20)];
mean4a=sortrows(mean4a,20);
y=mean4a(:,1);
yc=mean4a(:,1)+grandmeany;
x1=ones(n,m)*-999; xc1=ones(n,m)*-999;
for i=1:1:m
    x1(:,i)=mean4a(:,i+1);
    xc1(:,i)=mean4a(:,i+1)+grandmeanx0(i);
end
x=[ones(n,1) x1];
xc=[ones(n,1) xc1];
olsb=(x'*x)\(x'*y);
olscb=(xc'*xc)\(xc'*yc);
y2=sortrows(y2,3);
y2=y2(:,1); % district mean
r0=olsb;
dmy=13;
a=0;
b=100;
tolv=10^-4;
% *********************ci**********************************
xmax=zeros(m+1,1); xmin=zeros(m+1,1);
   for i=1:1:m+1
       xmax(i)=max(x(:,i));
       xmin(i)=min(x(:,i));
   end    
xlimit=[xmax xmin];   
v=vsign(r0);
xtpredict=range(r0,xlimit,v,y2,dmy);
xtbv=sum(xtpredict(:,1)<-10^-4)+sum(xtpredict(:,2)>100+10^-4);
% **********************************************************
fprintf('\nReplication of the column 1 in Table 1, Hansford and Gomez (2010)\n')
fprintf('\nNumber of Boundary Violations: %3.0f\n', xtbv);
fprintf('\nPredicted Value by Varying     Min   Max\n');
fprintf('All Variables                  %4.1f  %4.1f\n',xtpredict(1,1),xtpredict(1,2));
fprintf('Constant                       %4.1f  %4.1f\n',xtpredict(2,1),xtpredict(2,2));
fprintf('Partisan Composition           %4.1f  %4.1f\n',xtpredict(3,1),xtpredict(3,2));
fprintf('Turnout                        %4.1f  %4.1f\n',xtpredict(4,1),xtpredict(4,2));
fprintf('Turnout*GOP Incumbent         %4.1f  %4.1f\n',xtpredict(5,1),xtpredict(5,2));
fprintf('Turnout*Partisan Composition  %4.1f  %4.1f\n',xtpredict(6,1),xtpredict(6,2));
fprintf('Yr52                           %4.1f  %4.1f\n',xtpredict(7,1),xtpredict(7,2));
fprintf('Yr56                           %4.1f  %4.1f\n',xtpredict(8,1),xtpredict(8,2));
fprintf('Yr60                           %4.1f  %4.1f\n',xtpredict(9,1),xtpredict(9,2));
fprintf('Yr64                           %4.1f  %4.1f\n',xtpredict(10,1),xtpredict(10,2));
fprintf('Yr68                           %4.1f  %4.1f\n',xtpredict(11,1),xtpredict(11,2));
fprintf('Yr72                           %4.1f  %4.1f\n',xtpredict(12,1),xtpredict(12,2));
fprintf('Yr76                           %4.1f  %4.1f\n',xtpredict(13,1),xtpredict(13,2));
fprintf('Yr80                           %4.1f  %4.1f\n',xtpredict(14,1),xtpredict(14,2));
fprintf('Yr84                           %4.1f  %4.1f\n',xtpredict(15,1),xtpredict(15,2));
fprintf('Yr88                           %4.1f  %4.1f\n',xtpredict(16,1),xtpredict(16,2));
fprintf('Yr92                           %4.1f  %4.1f\n',xtpredict(17,1),xtpredict(17,2));
fprintf('Yr96                           %4.1f  %4.1f\n',xtpredict(18,1),xtpredict(18,2));
fprintf('Yr2000                         %4.1f  %4.1f\n\n',xtpredict(19,1),xtpredict(19,2));
